# /**
# * Copyright (c) 2022 Beijing Jiaotong University
# * PhotLab is licensed under [Open Source License].
# * You can use this software according to the terms and conditions of the [Open Source License].
# *
# * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
# *
# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# *
# * See the [Open Source License] for more details.
# */
# /**
# * Author: Shihao Xie
# * Created: Sep. 21, 2022
# * Supported by: National Key Research and Development Program of China
# */

import sqlite3
import re
import os
import time

FILE_PATTERN = re.compile(".+_data_type.thrift")

STRUCT_PATTERN = re.compile("struct.+\{[\s\S]*?\}")

STRUCT_NAME_PATTERN = re.compile("(?<=struct )\w*(?=\{)")

STRUCT_ATTRIBUTE_PATTERN = re.compile("([0-9]+):(required|optional)\s*(\S+)\s*(\w+)")

CREATE_ATTRIBUTE_TABLE_SQL = "CREATE TABLE IF NOT EXISTS {}(" \
                             "id INT PRIMARY KEY," \
                             "attribute_restrict VARCHAR (255)," \
                             "attribute_type VARCHAR (255)," \
                             "attribute_name VARCHAR (255)" \
                             ")"

DELETE_ATTRIBUTE_TABLE_SQL = "DROP TABLE {}"

INSERT_ATTRIBUTE_SQL = "INSERT OR REPLACE INTO {}" \
                       "(id, attribute_restrict, attribute_type, attribute_name) " \
                       "VALUES({}, {}, {}, {})"

UPDATE_ATTRIBUTE_SQL = "UPDATE {} " \
                       "SET id = {}," \
                       "attribute_restrict = {}, " \
                       "attribute_type = {}," \
                       "attribute_name = {} " \
                       "WHERE id = {} "

SELECT_ALL_ATTRIBUTE_SQL = "SELECT id, attribute_restrict, attribute_type, attribute_name FROM {}"

SELECT_ATTRIBUTE_SQL = "SELECT id, attribute_restrict, attribute_type, attribute_name FROM {} WHERE id = {}"



def input_thrift_data_type():
    """
    This function is to add thrift data type definition into database
    :return:
    """
    # Open database
    connection = sqlite3.connect("thrift_data_type.db")

    # Travers data definition file in 'interface' directory
    for path in get_data_type_file_list():
        # Read file
        file_pointer = open(path, "r")
        file_string = file_pointer.read()

        # Find out all struct
        for struct_string in re.findall(STRUCT_PATTERN, file_string):
            struct_name = re.search(STRUCT_NAME_PATTERN, struct_string).group()

            # Create data table named as struct name
            connection.execute(CREATE_ATTRIBUTE_TABLE_SQL.format(struct_name))
            cursor = connection.cursor()

            # Insert struct attributes into table
            struct_attribute = re.findall(STRUCT_ATTRIBUTE_PATTERN, struct_string)
            for attribute in struct_attribute:
                cursor.execute(INSERT_ATTRIBUTE_SQL.format(struct_name, "?", "?", "?", "?"), attribute)
            cursor.close()

    # Save to database
    connection.commit()
    connection.close()


def get_data_type_file_list():
    interface_dir = os.path.pardir + os.sep + "interface"
    file_list = os.listdir(interface_dir)
    data_type_list = []

    for file_name in file_list:
        if re.match(FILE_PATTERN, file_name):
            data_type_list.append(interface_dir + os.sep + file_name)
    return data_type_list

def input_component_prototype_data(path):
    pass

import uuid
if __name__ == '__main__':
    # input_thrift_data_type()
    uid = uuid.uuid1()
    print(uid)
    print(uid.int)
    print(int(str.split(uid.__str__(), "-")[0], 16))
    print(time.time())

